package com.barbeque.mall.service.wx;

import com.barbeque.mall.bean.wx.goods.MarketCategoryBean;
import com.barbeque.mall.bean.wx.goods.MarketCategoryBeanExample;
import com.barbeque.mall.bean.wx.goods.WxGoodsCategoryVO;
import com.barbeque.mall.mapper.WxMarketCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Euphonium
 * @since 2022/07/21
 */
@Service
public class WxCategoryServiceImpl implements WxCategoryService {

    @Autowired
    WxMarketCategoryMapper wxMarketCategoryMapper;

    /**
     * 根据传入的类目ID，获取其本身/父类目/兄弟类目的信息
     * > 传入的是一级类目ID，则父类目为其本身，兄弟类目为其所有子类目(按id升序)，current类目为兄弟类目的第一个
     * > 传入的是二级类目ID，则父类目为其pid所指类目，兄弟类目为其父类目的所有子类目(按id升序)，current类目为其本身
     *
     * @param categoryId
     * @return com.barbeque.mall.bean.wx.goods.WxGoodsCategoryVO
     * @author Euphonium
     * @since 2022/07/22 10:05
     */
    @Override
    public WxGoodsCategoryVO getGoodsCategory(Integer categoryId) {
        Integer pid = wxMarketCategoryMapper.selectPidByPrimaryKey(categoryId);
        if (pid == 0) {
            // 传入的是一级类目ID，则将一级类目ID传入
            return getGoodsCategoryRelation(categoryId);
        } else {
            // 传入的是二级类目ID，则将该类目的弗雷姆ID传入
            return getGoodsCategoryRelation(pid);
        }

    }

    /**
     * 根据pid，找到parentCategory(pid as id)、brotherCategory、currentCategory(order by id asec)
     *
     * @param pid
     * @return com.barbeque.mall.bean.wx.goods.WxGoodsCategoryVO
     * @author Euphonium
     * @since 2022/07/22 10:26
     */
    private WxGoodsCategoryVO getGoodsCategoryRelation(Integer pid) {
        WxGoodsCategoryVO wxGoodsCategoryVO = new WxGoodsCategoryVO();
        MarketCategoryBean parentCategory = wxMarketCategoryMapper.selectByPrimaryKey(pid);
        wxGoodsCategoryVO.setParentCategory(parentCategory);
        MarketCategoryBeanExample example = new MarketCategoryBeanExample();
        example.createCriteria().andPidEqualTo(pid);
        example.setOrderByClause("id asc");
        List<MarketCategoryBean> brotherCategory = wxMarketCategoryMapper.selectByExample(example);
        wxGoodsCategoryVO.setBrotherCategory(brotherCategory);
        wxGoodsCategoryVO.setCurrentCategory(brotherCategory.get(0));
        return wxGoodsCategoryVO;
    }

}
